# CardInteraction.gd 类脚本（卡牌互动接口，如点击、悬停、消除等）
class_name CardInteraction

const MIN_DRAG_DISTANCE := 10.0

var card_effect_resolver :CardEffectResolver = CardEffectResolver.new()
var chain_handler :ChainHandler = ChainHandler.new()
var card_manager :CardManager = CardManager.new()
#悬停碰撞区检测
static var is_hovering_on_card : bool = false
var hovering_card :CardBase
var next_hover_card :CardBase


func handle_click(card: CardBase) -> void:
	if _is_double_click():
		_process_double_click(card)
	else:
		_process_single_click(card)
		#_on_card_clicked(card)

static func _is_double_click() -> bool:
	return Input.get_mouse_button_mask() & MOUSE_BUTTON_MASK_LEFT and \
		Time.get_ticks_msec() - Input.get_last_mouse_velocity().x < 300

static func _process_double_click(card: CardBase) -> void:
	if card.card_data.type == CardData.Type.RESOURCE:
		_EconomyManager.add_resource("eco_point", card.card_data.eco_value * 2)

func _process_single_click(card: CardBase) -> void:
	_on_card_clicked(card)
	
# 卡牌点击处理
func _on_card_clicked(card: CardBase) -> void:
	#print("卡牌点击处理:", card.get_instance_id())
	if not _check_elimination_condition(card): #消除机制检查
		return
	await _process_elimination(card)
	chain_handler.update_chain_counter(card.card_data.type)
	card_effect_resolver.reveal_adjacent_cards(card)
	
# 消除条件验证
static func _check_elimination_condition(card: CardBase) -> bool:
	# 处理卡牌消除前的条件检验
	if card == null : return false
	var is_active := card.current_state == CardBase.State.ACTIVE
	return is_active
# 处理消除逻辑
static func _process_elimination(card: CardBase) -> void:
	await card.eliminate(card)
	#发送消除信号
	_EventBus.card_eliminated.emit(card)
	
# 处理悬停逻辑
func on_card_hovered(card:CardBase) :
	#print("card: ",card," hovered")
	if !is_hovering_on_card and !card_manager.card_being_dragged:
		is_hovering_on_card = true
		card_effect_resolver.highlight_card(card,true)
		hovering_card = card
	else :
		next_hover_card = card
# 处理悬停离开逻辑
func on_card_hovered_off(card:CardBase) :
	if !card_manager.card_being_dragged:
		if next_hover_card == card:
			next_hover_card = null
			
		if hovering_card == card :
			card_effect_resolver.highlight_card(card,false)
			is_hovering_on_card = false
			hovering_card = null
			if next_hover_card :
				_EventBus.card_hovered.emit(next_hover_card)
		else :
			card_effect_resolver.highlight_card(card,false)
	
